using System;
using mojoPortal.Web;
using mojoPortal.Web.Framework;
using Resources;

namespace ud_SwirlingQuestion
{
    public partial class SwirlingQuestionLoaderModule : SiteModuleControl
    {
        private int loadedModuleId = -1;
        private int loadedPageId = -1;
        private SwirlingQuestionConfiguration config = new SwirlingQuestionConfiguration();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Load += Page_Load;
            EnableViewState = false;
        }

        protected virtual void Page_Load(object sender, EventArgs e)
        {
            LoadSettings();
            PopulateLabels();
            PopulateControls();
        }

        private void PopulateControls()
        {
            if (config.ShowSearchPlace)
            {
                SearchPlace.Config = config;
                SearchPlace.PageId = loadedPageId;
                SearchPlace.ModuleId = loadedModuleId;
                SearchPlace.SiteRoot = SiteRoot;
            }
            QuestionList.ModuleId = loadedModuleId;
            QuestionList.PageId = loadedPageId;
            QuestionList.Config = config;
            QuestionList.SiteRoot = SiteRoot;
            QuestionList.ImageSiteRoot = ImageSiteRoot;
            QuestionList.IsEditable = IsEditable;
        }

        protected virtual void PopulateLabels()
        {
            Title1.ShowEditLinkOverride = false;
            if (IsEditable)
            {
                Title1.LiteralExtraMarkup +=
                    "&nbsp;<a href='"
                    + SiteRoot
                    + "/SwirlingQuestion/Admin/SwirlingQuestionEdit.aspx?pageid=" + loadedPageId.ToInvariantString()
                    + "&amp;mid=" + loadedModuleId.ToInvariantString()
                    + "' class='ModuleEditLink' title='" + SwirlingQuestionResource.ManageQuestion +
                    "'><img src='/Data/SiteImages/pencil.png' alt='" + SwirlingQuestionResource.ManageQuestion +
                    " |'</img></a>";
            }
            if (config.UsePostQuestionInTitleControlSetting)
            {
                Title1.LiteralExtraMarkup +=
                    "&nbsp;<a href='"
                    + SiteRoot
                    + "/SwirlingQuestion/PostQuestion.aspx?pageid=" + PageId.ToInvariantString()
                    + "&amp;mid=" + ModuleId.ToInvariantString()
                    + "' class='ModuleEditLink' title='" + SwirlingQuestionResource.AddQuestionLabel +
                    "'><img src='/Data/Icon16x16/Modify.png' alt='" + SwirlingQuestionResource.AddQuestionLabel +
                    " |'</img></a>";
            }
        }

        protected virtual void LoadSettings()
        {
            pnlContainer.ModuleId = ModuleId;
            config = new SwirlingQuestionConfiguration(Settings);
            if (!config.CustomCssClass.Equals(string.Empty)) { pnlContainer.CssClass += " " + config.CustomCssClass; }
            if (!config.ModuleLoader.Equals(string.Empty))
            {
                string loadedModule = config.ModuleLoader;
                if (!loadedModule.Equals(string.Empty))
                {
                    int.TryParse(loadedModule.Remove(loadedModule.IndexOf("-")), out loadedModuleId);
                    int.TryParse(loadedModule.Remove(0, loadedModule.IndexOf("-") + 1), out loadedPageId);
                }
            }
            else
            {
                loadedModuleId = 0;
                loadedPageId = 0;
            }
            SearchPlace.Visible = config.ShowSearchPlace;
        }

    }
}
